<template>
  <div class="login">
    <van-nav-bar title="个人登录页" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="userName"
        name="userName"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
    <router-link :to="{ name: 'Reg' }">没有账号去注册</router-link>
  </div>
</template>

<script>
import { Notify } from 'vant';
import {loginAPI} from "../services/reg"
import { setToken } from "../utils/auth";
// import { mapActions, mapState } from "vuex";
export default {
  data(){
    return {
      userName:"",
      password:"",
    }
  },
  created(){
      // this.loadData();
  },
  computed:{
      // ...mapState("cartsCount", ["cartInfo"]),
  },
  methods:{
      // loadData() {
      //  this.getcars();
      // },
    // ...mapActions("cartsCount", {getcars:"getcars"}),
   async onSubmit(){
      const res = await loginAPI({
        userName: this.userName,
        password: this.password,
      })
        if (res.code == 1) {
        Notify({
          message: "登录成功",
          type: "success",
        });
        setToken(res.data);
        this.$store.dispatch("cartsCount/getcars");
        // ...mapActions("cartsCount", {getcars:"getcars"}),
        this.$router.replace({
            name: "User",
            query:{isshow:true},
        });
      } else {
        Notify({
          message: res.data,
          type: "warning",
        });
      }

    }
  }
}

</script>

<style></style>
